home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / vsoup11.zip / util.cc < prev    next >
C/C++ Source or Header  |  1996-09-02  |  5KB  |  218 lines

  1. //  $Id: util.cc 1.9 1996/09/02 13:30:55 hardy Exp $
  2. //
  3. //  This progam/module was written by Hardy Griech based on ideas and
  4. //  pieces of code from Chin Huang (cthuang@io.org).  Bug reports should
  5. //  be submitted to rgriech@ibm.net.
  6. //
  7. //  This file is part of soup++ for OS/2.  Soup++ including this file
  8. //  is freeware.  There is no warranty of any kind implied.  The terms
  9. //  of the GNU Gernal Public Licence are valid for this piece of software.
  10. //
  11. //  NNTP client routines
  12. //
  13.  
  14.  
  15. #include <ctype.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #include "mts.hh"
  20. #include "util.hh"
  21.  
  22.  
  23.  
  24. unsigned hashi( const char *src, unsigned tabSize )
  25. {
  26.     unsigned long res = 0;
  27.     const char *p;
  28.     const unsigned long maxbit  = 0x40000000;   // needs 32bits
  29.     const unsigned long maxmask = maxbit - 1;
  30.  
  31.     for (p = src;  *p != '\0';  ++p) {
  32.     res = (res << 1) ^ tolower(*p);
  33.     if (res >= maxbit)
  34.         res = (res & maxmask) ^ 0x00000001;
  35.     }
  36.     return res % tabSize;
  37. }   // hashi
  38.  
  39.  
  40.  
  41. int isHeader( const char *buf, const char *header )
  42. {
  43.     size_t len = strlen(header);
  44.     return strnicmp(buf, header, len) == 0  &&  buf[len] == ':'  &&
  45.     (buf[len+1] == ' ' || buf[len+1] == '\t');
  46. }   // isHeader
  47.  
  48.  
  49.  
  50. const char *getHeader( FILE *fd, const char *headerField )
  51. //
  52. //  get the value for the message header field (NULL, if not found)
  53. //  the memory for the result is allocated from the heap!
  54. //  *fd is file that contains header/message info
  55. //  on return the file is positioned to its original position
  56. //
  57. {
  58.     char buf[BUFSIZ];
  59.     char *result;
  60.     long offset;
  61.     int headerFieldLen, n;
  62.  
  63.     /* Remember file position */
  64.     offset = ftellT(fd);
  65.  
  66.     headerFieldLen = strlen(headerField);
  67.  
  68.     //
  69.     //  Look through header
  70.     //
  71.     while (fgetsT(buf, sizeof(buf), fd)) {
  72.     //
  73.     //  end of header ?
  74.     //
  75.     if (buf[0] == '\n')
  76.         break;
  77.  
  78.     //
  79.     //  is there a match with headerField
  80.     //
  81.     if (isHeader(buf,headerField)) {
  82.         //
  83.         //  yes -> allocate memory for result and copy info of headerField
  84.         //         to result
  85.         //
  86.         n = strlen(buf + headerFieldLen + 2);
  87.         result = (char *)xstrdup(buf+headerFieldLen+2);
  88.         if (result[n-1] == '\n')
  89.         result[n-1] = '\0';
  90.         fseekT(fd, offset, SEEK_SET);          //  reposition file
  91.         return result;
  92.     }
  93.     }       
  94.  
  95.     //
  96.     //  Reposition file
  97.     //
  98.     fseekT(fd, offset, SEEK_SET);
  99.     return NULL;
  100. }   // getHeader
  101.  
  102.  
  103.  
  104. const char *extractAddress( const char *src )
  105. //
  106. //  Extract mail address from the string.
  107. //  Return a pointer to buffer on the heap containing the address or
  108. //  NULL on an error.
  109. //
  110. {
  111.     char buf[BUFSIZ];
  112.     char ch, *put;
  113.     const char *get;
  114.     char gotAddress;
  115.  
  116.     gotAddress = 0;
  117.     put = buf;
  118.     if ((get = strchr(src, '<')) != 0) {
  119.     char ch = *++get;
  120.     while (ch != '>' && ch != '\0') {
  121.         *put++ = ch;
  122.         ch = *++get;
  123.     }
  124.     gotAddress = 1;
  125.     } else {
  126.     get = src;
  127.     ch = *get++;
  128.  
  129.     /* Skip leading whitespace. */
  130.     while (ch != '\0' && isspace(ch))
  131.         ch = *get++;
  132.  
  133.     while (ch != '\0') {
  134.         if (isspace(ch)) {
  135.         ch = *get++;
  136.  
  137.         } else if (ch == '(') {
  138.         /* Skip comment. */
  139.         int nest = 1;
  140.         while (nest > 0 && ch != '\0') {
  141.             ch = *get++;
  142.  
  143.             if (ch == '(')
  144.             ++nest;
  145.             else if (ch == ')')
  146.             --nest;
  147.         }
  148.  
  149.         if (ch == ')') {
  150.             ch = *get++;
  151.         }
  152.  
  153.         } else if (ch == '"') {
  154.         /* Copy quoted string. */
  155.         do {
  156.             *put++ = ch;
  157.             ch = *get++;
  158.         } while (ch != '"' && ch != '\0');
  159.  
  160.         if (ch == '"') {
  161.             *put++ = ch;
  162.             ch = *get++;
  163.         }
  164.  
  165.         } else {
  166.         /* Copy address. */
  167.         while (ch != '\0' && ch != '(' && !isspace(ch)) {
  168.             *put++ = ch;
  169.             ch = *get++;
  170.         }
  171.         gotAddress = 1;
  172.         }
  173.     }
  174.     }
  175.  
  176.     if (gotAddress) {
  177.     *put = '\0';
  178.     return xstrdup( buf );
  179.     } else {
  180.     return NULL;
  181.     }
  182. }   // extractAddress
  183.  
  184.  
  185.  
  186. char *findAddressSep( const char *src )
  187. //
  188. //  Search for ',' separating addresses.
  189. //
  190. {
  191.     char ch, matchCh;
  192.  
  193.     ch = *src; 
  194.     while (ch != '\0' && ch != ',') {
  195.         if (ch == '"') {
  196.             matchCh = '"';
  197.         } else if (ch == '(') {
  198.             matchCh = ')';
  199.         } else if (ch == '<') {
  200.             matchCh = '>';
  201.         } else {
  202.             matchCh = '\0';
  203.         }
  204.  
  205.         if (matchCh) {
  206.             do {
  207.                 ch = *(++src);
  208.             } while (ch != '\0' && ch != matchCh);
  209.  
  210.             if (ch == '\0')
  211.                 break;
  212.         }
  213.         ch = *(++src);
  214.     }
  215.  
  216.     return (char *)src;
  217. }   // findAdressSep
  218.